package com.it.common.utils

import android.app.Dialog
import android.content.Context
import android.graphics.Color
import android.os.Build
import android.view.*
import android.widget.LinearLayout
import androidx.annotation.LayoutRes
import com.yili.common.R


/**
 * 对话框工具类
 */
class DialogUtils(context: Context, style: Int? = null) :
    Dialog(context, style ?: R.style.my_dialog_style) {

    init {
        // 解决进度弹窗出现时，状态栏变成黑色
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            val window = this.window
            window?.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
            window?.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
            window?.statusBarColor = Color.TRANSPARENT
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                try {
                    val decorViewClazz =
                        Class.forName("com.android.internal.policy.DecorView")
                    val field = decorViewClazz.getDeclaredField("mSemiTransparentStatusBarColor")
                    field.isAccessible = true
                    field.setInt(window?.decorView, Color.TRANSPARENT)
                } catch (e: Exception) {
                }
            }
        }
    }

    /**
     * @param content
     * @param resource 传入布局ID
     * @return
     */
    companion object {
        fun getDialog(content: Context, @LayoutRes resource: Int, style: Int? = null): DialogUtils {
            val dialog = DialogUtils(content, style)
            val view =
                LayoutInflater.from(content).inflate(resource, LinearLayout(content), false)
            view.setOnClickListener { dialog.dismiss() }
            dialog.setContentView(view)
            val win = dialog.window
            win?.decorView?.setPadding(0, 0, 0, 0)
            val lp = win?.attributes
            lp?.width = WindowManager.LayoutParams.MATCH_PARENT
            lp?.height = WindowManager.LayoutParams.MATCH_PARENT
            return dialog
        }

        /**
         * @param content
         * @param resource
         * @return
         */
        fun getDialogNotTouchOutside(
            content: Context,
            @LayoutRes resource: Int,
            style: Int? = null
        ): DialogUtils {
            val dialog = DialogUtils(content,style)
            val view =
                LayoutInflater.from(content).inflate(resource, LinearLayout(content), false)
            dialog.setCancelable(true)
            dialog.setCanceledOnTouchOutside(false)
            dialog.setContentView(view)

            val win = dialog.window
            win?.decorView?.setPadding(0, 0, 0, 0)
            val lp = win?.attributes
            lp?.width = WindowManager.LayoutParams.MATCH_PARENT
            lp?.height = WindowManager.LayoutParams.MATCH_PARENT
            win?.attributes = lp

            return dialog
        }

        /**
         * @param content
         * @param resource
         * @return
         */
        fun getDialogNotTouchOutside(content: Context, view: View,      style: Int? = null): DialogUtils {
            val dialog = DialogUtils(content,style)
            dialog.setCancelable(true)
            dialog.setCanceledOnTouchOutside(false)
            dialog.setContentView(view)
            val win = dialog.window
            win?.decorView?.setPadding(0, 0, 0, 0)
            val lp = win?.attributes
            lp?.width = WindowManager.LayoutParams.MATCH_PARENT
            lp?.height = WindowManager.LayoutParams.MATCH_PARENT
            win?.attributes = lp

            return dialog
        }
        /**
         * @param content
         * @param resource
         * @return
         */
        fun getDialog(content: Context, view: View, style: Int? = null): DialogUtils {
            val dialog = DialogUtils(content,style)
            dialog.setCancelable(true)
            dialog.setCanceledOnTouchOutside(true)
            dialog.setContentView(view)
            val win = dialog.window
            win?.decorView?.setPadding(0, 0, 0, 0)
            val lp = win?.attributes
            lp?.width = WindowManager.LayoutParams.MATCH_PARENT
            lp?.height = WindowManager.LayoutParams.MATCH_PARENT
            win?.attributes = lp

            return dialog
        }

    }


    override fun show() {
        val window = window
        focusNotAle(window!!)
        super.show()
        hideNavigationBar(window)
        clearFocusNotAle(window)
    }

    /**
     * 隐藏虚拟栏 ，显示的时候再隐藏掉
     *
     * @param window
     */
    fun hideNavigationBar(window: Window) {
        // 隐藏状态栏和导航栏
        window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_FULLSCREEN or
                View.SYSTEM_UI_FLAG_HIDE_NAVIGATION or View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
    }


    /**
     * dialog 需要全屏的时候用，和clearFocusNotAle() 成对出现
     * 在show 前调用  focusNotAle   show后调用clearFocusNotAle
     *
     * @param window
     */
    fun focusNotAle(window: Window) {
        window.setFlags(
            WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
            WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
        )
    }

    /**
     * dialog 需要全屏的时候用，focusNotAle() 成对出现
     * 在show 前调用  focusNotAle   show后调用clearFocusNotAle
     *
     * @param window
     */
    fun clearFocusNotAle(window: Window) {
        window.clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE)
    }


}
